package com.web.mall.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.web.mall.entity.TbBrand;
import com.web.mall.entity.TbSpecification;
import com.web.mall.entity.TbSpecificationDto;
import com.web.mall.entity.TbSpecificationOption;
import com.web.mall.req.TbBrandReq;
import com.web.mall.req.TbSpecificationReq;
import com.web.mall.service.ITbSpecificationOptionService;
import com.web.mall.service.ITbSpecificationService;
import com.web.mall.utils.ResultBean;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author s1x
 * @since 2022-12-08
 */
@RestController
@RequestMapping("/spec")
public class TbSpecificationController {
    @Autowired
    ITbSpecificationService tbSpecificationService;
    @Autowired
    ITbSpecificationOptionService tbSpecificationOptionService;

    // 分页条件查询
    @GetMapping("/getByCondition")
    public ResultBean getByCondition(TbSpecificationReq tbSpecification) {
        System.out.println("\n分页条件查询tbSpecification:" + tbSpecification);
        //1. 分页拦截，重构分页sql语句
        Page pageInfo = new Page(tbSpecification.getPage(), tbSpecification.getSize());
        LambdaQueryWrapper<TbSpecification> wrapper = new LambdaQueryWrapper();
        wrapper.like(StringUtils.isNotEmpty(tbSpecification.getSpecName()), TbSpecification::getSpecName, tbSpecification.getSpecName());

        tbSpecificationService.page(pageInfo, wrapper);
        System.out.println("分页pageInfo:" + pageInfo);
        return ResultBean.ok(pageInfo);
    }


    // 查询全部
    @GetMapping("/getAll")
    public ResultBean getAll() {
        return ResultBean.ok(tbSpecificationService.list());
    }


    // 根据id删除
    @Transactional
    @GetMapping("/delete/{id}")
    public ResultBean delete(@PathVariable("id") Long id) {
        // 删除规格
        tbSpecificationService.removeById(id);

        // 删除规格选项
        LambdaQueryWrapper<TbSpecificationOption> wrapper = new LambdaQueryWrapper();
        wrapper.eq(!ObjectUtils.isEmpty(id), TbSpecificationOption::getSpecId, id);
        tbSpecificationOptionService.remove(wrapper);
        return ResultBean.ok();
    }

    // 新增或更新
    @Transactional
    @PostMapping("/addOrUpdate")
    public ResultBean addOrUpdate(@RequestBody TbSpecificationDto specification) {
        System.out.println("新增或更新specification：" + specification);
        // 新增或更新规格
        TbSpecification spec = new TbSpecification();
        spec.setId(specification.getId());
        spec.setSpecName(specification.getSpecName());
        tbSpecificationService.saveOrUpdate(spec);
        System.out.println("\n新增或更新完毕后的spec：" + spec);

        if (specification.getTbSpecificationOptions() != null) {
            // 先根据规格id删除规格选项
            LambdaQueryWrapper<TbSpecificationOption> wrapper = new LambdaQueryWrapper();
            wrapper.eq(!ObjectUtils.isEmpty(spec.getId()), TbSpecificationOption::getSpecId, spec.getId());
            tbSpecificationOptionService.remove(wrapper);

            // 补充规格的id
            for (TbSpecificationOption s : specification.getTbSpecificationOptions()) {
                s.setSpecId(spec.getId());
            }

            // 再添加规格选项
            tbSpecificationOptionService.saveBatch(specification.getTbSpecificationOptions());
        }
        return ResultBean.ok();
    }
}
